package com.example.springsouress.com.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * -- Created by IntelliJ IDEA.
 * -- REMARK:
 * -- User: tanwei
 * -- Date: 2020/12/10
 * -- Time: 1:49 下午
 * --
 */
public class ListUtils {
    public static <T> List<List<T>> parallelList(List<T> list, int toIndex) {
        int i = list.size() / toIndex;
        if (0 != list.size() % toIndex) {
            ++i;
        }
        List<List<T>> listList = Stream.iterate(0, n -> n + 1).limit(i).parallel().map(a -> {
            List<T> collect = list.stream().skip(a * toIndex).limit(toIndex).parallel().collect(Collectors.toList());
            return collect;
        }).collect(Collectors.toList());
        return listList;
    }

    public static <T> List<List<T>> groupList(List<T> list, int toIndex) {
        int listSize = list.size();
        ArrayList<List<T>> arrayList = new ArrayList<>();
        int keyToken = 0;
        for (int i = 0; i < list.size(); i += toIndex) {
            if (i + toIndex > listSize) {
                toIndex = listSize - i;
            }
            arrayList.add(list.subList(i, i + toIndex));

            keyToken++;
        }
        return arrayList;
    }

}
